import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.io.OutputStream;
import java.io.InputStream;


public class TcpServer1 {
    public static void main(String[] args) throws Exception{
        //创建服务器对象 指定程序端口
        ServerSocket server=new ServerSocket(8888);
        System.out.println("服务器："+server);
        //
        Socket client=server.accept();
        System.out.println("客户端连接成功："+client);
        //创建IO流对象
        InputStream is = client.getInputStream();
        OutputStream os = client.getOutputStream();
        byte[] b = new byte[1024];
        int length = is.read(b);
        if(length != -1){
            //输出读到的内容
            System.out.println(new String(b,0,length));
        }
         // 创建一个 StringBuilder 对象，并将原始字符串传入
        StringBuilder stringBuilder = new StringBuilder(new String(b,0,length));
        // 调用 reverse() 方法反转字符串
        String reversedString = stringBuilder.reverse().toString();
        byte[] b1 = reversedString.getBytes();
        os.write(b1);
        //关闭资源
        is.close();
        os.close();
        client.close();
        server.close();
    }
}